
// Compiler implementation of the D programming language
// Copyright (c) 1999-2008 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "../l_service/c_ds/rmem.h"

#include "statement.h"
#include "expression.h"
#include "cond.h"
#include "init.h"
#include "staticassert.h"
#include "mtype.h"
#include "scope.h"
#include "declaration.h"
#include "aggregate.h"
#include "../l_neo_lexer/id.h"

#define LOG	0

struct InterState
{
    InterState *caller;		// calling function's InterState
    P_FuncDeclaration *fd;	// function being interpreted
    Dsymbols vars;		// variables used in this function
    S_Statement *start;		// if !=NULL, start execution at this statement
    S_Statement *gotoTarget;	// target of EXP_GOTO_INTERPRET result

    InterState();
};

InterState::InterState()
{
    memset(this, 0, sizeof(InterState));
}

E_Expression *interpret_aaLen(InterState *istate, Expressions *arguments);
E_Expression *interpret_aaKeys(InterState *istate, Expressions *arguments);
E_Expression *interpret_aaValues(InterState *istate, Expressions *arguments);

/*************************************
 * Attempt to interpret a function given the arguments.
 * Input:
 *	istate	state for calling function (NULL if none)
 * Return result expression if successful, NULL if not.
 */

E_Expression *P_FuncDeclaration::interpret(InterState *istate, Expressions *arguments)
{
#if LOG
    printf("\n********\nFuncDeclaration::interpret(istate = %p) %s\n", istate, oToChars());
    printf("cantInterpret = %d, semanticRun = %d\n", cantInterpret, semanticRun);
#endif
    if (global.errors)
	return NULL;
    if (ident == Id::aaLen)
	return interpret_aaLen(istate, arguments);
    else if (ident == Id::aaKeys)
	return interpret_aaKeys(istate, arguments);
    else if (ident == Id::aaValues)
	return interpret_aaValues(istate, arguments);

    if (cantInterpret || semanticRun == 1)
	return NULL;

    if (needThis() || isNested() || !fbody)
    {	cantInterpret = 1;
	return NULL;
    }

    if (semanticRun == 0 && scope)
    {
	semantic3(scope);
	if (global.errors)	// if errors compiling this function
	    return NULL;
    }
    if (semanticRun < 2)
	return NULL;

    Type *tb = type->toBasetype();
    assert(tb->ty == Tfunction);
    TypeFunction *tf = (TypeFunction *)tb;
    Type *tret = tf->next->toBasetype();
    if (tf->varargs /*|| tret->ty == Tvoid*/)
    {	cantInterpret = 1;
	return NULL;
    }

    if (tf->parameters)
    {	size_t dim = Argument::dim(tf->parameters);
	for (size_t i = 0; i < dim; i++)
	{   Argument *arg = Argument::getNth(tf->parameters, i);
	    if (arg->storageClass & STClazy)
	    {   cantInterpret = 1;
		return NULL;
	    }
	}
    }

    InterState istatex;
    istatex.caller = istate;
    istatex.fd = this;

    Expressions vsave;		// place to save previous parameter values
    size_t dim = 0;
    if (arguments)
    {
	dim = arguments->dim;
	assert(!dim || parameters->dim == dim);
	vsave.setDim(dim);

	/* Evaluate all the arguments to the function,
	 * store the results in eargs[]
	 */
	Expressions eargs;
	eargs.setDim(dim);

	for (size_t i = 0; i < dim; i++)
	{   E_Expression *earg = (E_Expression *)arguments->data[i];
	    Argument *arg = Argument::getNth(tf->parameters, i);

	    if (arg->storageClass & (STCout | STCref))
	    {
	    }
	    else
	    {	/* Value parameters
		 */
		Type *ta = arg->type->toBasetype();
		if (ta->ty == Tsarray && earg->op == LT::TOKaddress)
		{
		    /* Static arrays are passed by a simple pointer.
		     * Skip past this to get at the actual arg.
		     */
		    earg = ((E_AddrExp *)earg)->e1;
		}
		earg = earg->interpret(istate ? istate : &istatex);
		if (earg == EXP_CANT_INTERPRET)
		    return NULL;
	    }
	    eargs.data[i] = earg;
	}

	for (size_t i = 0; i < dim; i++)
	{   E_Expression *earg = (E_Expression *)eargs.data[i];
	    Argument *arg = Argument::getNth(tf->parameters, i);
	    P_VarDeclaration *v = (P_VarDeclaration *)parameters->data[i];
	    vsave.data[i] = v->value;
#if LOG
	    printf("arg[%d] = %s\n", i, earg->oToChars());
#endif
	    if (arg->storageClass & (STCout | STCref))
	    {
		/* Bind out or ref parameter to the corresponding
		 * variable v2
		 */
		if (!istate || earg->op != LT::TOKvar)
		    return NULL;	// can't bind to non-interpreted vars

		P_VarDeclaration *v2;
		while (1)
		{
		    E_VarExp *ve = (E_VarExp *)earg;
		    v2 = ve->var->isVarDeclaration();
		    if (!v2)
			return NULL;
		    if (!v2->value || v2->value->op != LT::TOKvar)
			break;
		    earg = v2->value;
		}

		v->value = new E_VarExp(earg->loc, v2);

		/* Don't restore the value of v2 upon function return
		 */
		assert(istate);
		for (size_t i = 0; i < istate->vars.dim; i++)
		{   P_VarDeclaration *v = (P_VarDeclaration *)istate->vars.data[i];
		    if (v == v2)
		    {	istate->vars.data[i] = NULL;
			break;
		    }
		}
	    }
	    else
	    {	/* Value parameters
		 */
		v->value = earg;
	    }
#if LOG
	    printf("interpreted arg[%d] = %s\n", i, earg->oToChars());
#endif
	}
    }

    /* Save the values of the local variables used
     */
    Expressions valueSaves;
    if (istate)
    {
	//printf("saving local variables...\n");
	valueSaves.setDim(istate->vars.dim);
	for (size_t i = 0; i < istate->vars.dim; i++)
	{   P_VarDeclaration *v = (P_VarDeclaration *)istate->vars.data[i];
	    if (v)
	    {
		//printf("\tsaving [%d] %s = %s\n", i, v->oToChars(), v->value ? v->value->oToChars() : "");
		valueSaves.data[i] = v->value;
		v->value = NULL;
	    }
	}
    }

    E_Expression *e = NULL;

    while (1)
    {
	e = fbody->interpret(&istatex);
	if (e == EXP_CANT_INTERPRET)
	{
#if LOG
	    printf("function body failed to interpret\n");
#endif
	    e = NULL;
	}

	/* This is how we deal with a recursive statement AST
	 * that has arbitrary goto statements in it.
	 * Bubble up a 'result' which is the target of the goto
	 * statement, then go recursively down the AST looking
	 * for that statement, then execute starting there.
	 */
	if (e == EXP_GOTO_INTERPRET)
	{
	    istatex.start = istatex.gotoTarget;	// set starting statement
	    istatex.gotoTarget = NULL;
	}
	else
	    break;
    }

    /* Restore the parameter values
     */
    for (size_t i = 0; i < dim; i++)
    {
	P_VarDeclaration *v = (P_VarDeclaration *)parameters->data[i];
	v->value = (E_Expression *)vsave.data[i];
    }

    if (istate)
    {
	/* Restore the variable values
	 */
	//printf("restoring local variables...\n");
	for (size_t i = 0; i < istate->vars.dim; i++)
	{   P_VarDeclaration *v = (P_VarDeclaration *)istate->vars.data[i];
	    if (v)
	    {	v->value = (E_Expression *)valueSaves.data[i];
		//printf("\trestoring [%d] %s = %s\n", i, v->oToChars(), v->value ? v->value->oToChars() : "");
	    }
	}
    }

    return e;
}

/******************************** S_Statement ***************************/

#define START()				\
    if (istate->start)			\
    {	if (istate->start != this)	\
	    return NULL;		\
	istate->start = NULL;		\
    }

/***********************************
 * Interpret the statement.
 * Returns:
 *	NULL	continue to next statement
 *	EXP_CANT_INTERPRET	cannot interpret statement at compile time
 *	!NULL	expression from return statement
 */

E_Expression *S_Statement::interpret(InterState *istate)
{
#if LOG
    printf("S_Statement::interpret()\n");
#endif
    START()
    return EXP_CANT_INTERPRET;
}

E_Expression *S_ExpStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ExpStatement::interpret(%s)\n", exp ? exp->oToChars() : "");
#endif
    START()
    if (exp)
    {
	E_Expression *e = exp->interpret(istate);
	if (e == EXP_CANT_INTERPRET)
	{
	    //printf("-S_ExpStatement::interpret(): %p\n", e);
	    return EXP_CANT_INTERPRET;
	}
    }
    return NULL;
}

E_Expression *S_CompoundStatement::interpret(InterState *istate)
{   E_Expression *e = NULL;

#if LOG
    printf("S_CompoundStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    if (statements)
    {
	for (size_t i = 0; i < statements->dim; i++)
	{   S_Statement *s = (S_Statement *)statements->data[i];

	    if (s)
	    {
		e = s->interpret(istate);
		if (e)
		    break;
	    }
	}
    }
#if LOG
    printf("-S_CompoundStatement::interpret() %p\n", e);
#endif
    return e;
}

E_Expression *S_UnrolledLoopStatement::interpret(InterState *istate)
{   E_Expression *e = NULL;

#if LOG
    printf("S_UnrolledLoopStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    if (statements)
    {
	for (size_t i = 0; i < statements->dim; i++)
	{   S_Statement *s = (S_Statement *)statements->data[i];

	    e = s->interpret(istate);
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_CONTINUE_INTERPRET)
	    {	e = NULL;
		continue;
	    }
	    if (e == EXP_BREAK_INTERPRET)
	    {	e = NULL;
		break;
	    }
	    if (e)
		break;
	}
    }
    return e;
}

E_Expression *S_IfStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_IfStatement::interpret(%s)\n", condition->oToChars());
#endif

    if (istate->start == this)
	istate->start = NULL;
    if (istate->start)
    {
	E_Expression *e = NULL;
	if (ifbody)
	    e = ifbody->interpret(istate);
	if (istate->start && elsebody)
	    e = elsebody->interpret(istate);
	return e;
    }

    E_Expression *e = condition->interpret(istate);
    assert(e);
    //if (e == EXP_CANT_INTERPRET) printf("cannot interpret\n");
    if (e != EXP_CANT_INTERPRET)
    {
	if (e->isBool(TRUE))
	    e = ifbody ? ifbody->interpret(istate) : NULL;
	else if (e->isBool(FALSE))
	    e = elsebody ? elsebody->interpret(istate) : NULL;
	else
	{
	    e = EXP_CANT_INTERPRET;
	}
    }
    return e;
}

E_Expression *S_ScopeStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ScopeStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    return statement ? statement->interpret(istate) : NULL;
}

E_Expression *S_ReturnStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ReturnStatement::interpret(%s)\n", exp ? exp->oToChars() : "");
#endif
    START()
    if (!exp)
	return EXP_VOID_INTERPRET;
#if LOG
    E_Expression *e = exp->interpret(istate);
    printf("e = %p\n", e);
    return e;
#else
    return exp->interpret(istate);
#endif
}

E_Expression *S_BreakStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_BreakStatement::interpret()\n");
#endif
    START()
    if (ident)
	return EXP_CANT_INTERPRET;
    else
	return EXP_BREAK_INTERPRET;
}

E_Expression *S_ContinueStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ContinueStatement::interpret()\n");
#endif
    START()
    if (ident)
	return EXP_CANT_INTERPRET;
    else
	return EXP_CONTINUE_INTERPRET;
}

E_Expression *S_WhileStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_WhileStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    E_Expression *e;

    if (istate->start)
    {
	e = body ? body->interpret(istate) : NULL;
	if (istate->start)
	    return NULL;
	if (e == EXP_CANT_INTERPRET)
	    return e;
	if (e == EXP_BREAK_INTERPRET)
	    return NULL;
	if (e != EXP_CONTINUE_INTERPRET)
	    return e;
    }

    while (1)
    {
	e = condition->interpret(istate);
	if (e == EXP_CANT_INTERPRET)
	    break;
	if (!e->isConst())
	{   e = EXP_CANT_INTERPRET;
	    break;
	}
	if (e->isBool(TRUE))
	{   e = body ? body->interpret(istate) : NULL;
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_CONTINUE_INTERPRET)
		continue;
	    if (e == EXP_BREAK_INTERPRET)
	    {	e = NULL;
		break;
	    }
	    if (e)
		break;
	}
	else if (e->isBool(FALSE))
	{   e = NULL;
	    break;
	}
	else
	    assert(0);
    }
    return e;
}

E_Expression *S_DoStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_DoStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    E_Expression *e;

    if (istate->start)
    {
	e = body ? body->interpret(istate) : NULL;
	if (istate->start)
	    return NULL;
	if (e == EXP_CANT_INTERPRET)
	    return e;
	if (e == EXP_BREAK_INTERPRET)
	    return NULL;
	if (e == EXP_CONTINUE_INTERPRET)
	    goto Lcontinue;
	if (e)
	    return e;
    }

    while (1)
    {
	e = body ? body->interpret(istate) : NULL;
	if (e == EXP_CANT_INTERPRET)
	    break;
	if (e == EXP_BREAK_INTERPRET)
	{   e = NULL;
	    break;
	}
	if (e && e != EXP_CONTINUE_INTERPRET)
	    break;

    Lcontinue:
	e = condition->interpret(istate);
	if (e == EXP_CANT_INTERPRET)
	    break;
	if (!e->isConst())
	{   e = EXP_CANT_INTERPRET;
	    break;
	}
	if (e->isBool(TRUE))
	{
	}
	else if (e->isBool(FALSE))
	{   e = NULL;
	    break;
	}
	else
	    assert(0);
    }
    return e;
}

E_Expression *S_ForStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ForStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    E_Expression *e;

    if (init)
    {
	e = init->interpret(istate);
	if (e == EXP_CANT_INTERPRET)
	    return e;
	assert(!e);
    }

    if (istate->start)
    {
	e = body ? body->interpret(istate) : NULL;
	if (istate->start)
	    return NULL;
	if (e == EXP_CANT_INTERPRET)
	    return e;
	if (e == EXP_BREAK_INTERPRET)
	    return NULL;
	if (e == EXP_CONTINUE_INTERPRET)
	    goto Lcontinue;
	if (e)
	    return e;
    }

    while (1)
    {
	if (!condition)
	    goto Lhead;
	e = condition->interpret(istate);
	if (e == EXP_CANT_INTERPRET)
	    break;
	if (!e->isConst())
	{   e = EXP_CANT_INTERPRET;
	    break;
	}
	if (e->isBool(TRUE))
	{
	Lhead:
	    e = body ? body->interpret(istate) : NULL;
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_BREAK_INTERPRET)
	    {   e = NULL;
		break;
	    }
	    if (e && e != EXP_CONTINUE_INTERPRET)
		break;
	Lcontinue:
	    if (increment)
	    {
		e = increment->interpret(istate);
		if (e == EXP_CANT_INTERPRET)
		    break;
	    }
	}
	else if (e->isBool(FALSE))
	{   e = NULL;
	    break;
	}
	else
	    assert(0);
    }
    return e;
}

E_Expression *S_ForeachStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ForeachStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    if (istate->start)
	return NULL;

    E_Expression *e = NULL;
    E_Expression *eaggr;

    if (value->isOut() || value->isRef())
	return EXP_CANT_INTERPRET;

    eaggr = aggr->interpret(istate);
    if (eaggr == EXP_CANT_INTERPRET)
	return EXP_CANT_INTERPRET;

    E_Expression *dim = ArrayLength(Type::tsize_t, eaggr);
    if (dim == EXP_CANT_INTERPRET)
	return EXP_CANT_INTERPRET;

    E_Expression *keysave = key ? key->value : NULL;
    E_Expression *valuesave = value->value;

    uinteger_t d = dim->toUInteger();
    uinteger_t index;

    if (op == LT::TOKforeach)
    {
	for (index = 0; index < d; index++)
	{
	    E_Expression *ekey = new E_IntegerExp(loc, index, Type::tsize_t);
	    if (key)
		key->value = ekey;
	    e = Index(value->type, eaggr, ekey);
	    if (e == EXP_CANT_INTERPRET)
		break;
	    value->value = e;

	    e = body ? body->interpret(istate) : NULL;
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_BREAK_INTERPRET)
	    {   e = NULL;
		break;
	    }
	    if (e == EXP_CONTINUE_INTERPRET)
		e = NULL;
	    else if (e)
		break;
	}
    }
    else // LT::TOKforeach_reverse
    {
	for (index = d; index-- != 0;)
	{
	    E_Expression *ekey = new E_IntegerExp(loc, index, Type::tsize_t);
	    if (key)
		key->value = ekey;
	    e = Index(value->type, eaggr, ekey);
	    if (e == EXP_CANT_INTERPRET)
		break;
	    value->value = e;

	    e = body ? body->interpret(istate) : NULL;
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_BREAK_INTERPRET)
	    {   e = NULL;
		break;
	    }
	    if (e == EXP_CONTINUE_INTERPRET)
		e = NULL;
	    else if (e)
		break;
	}
    }
    value->value = valuesave;
    if (key)
	key->value = keysave;
    return e;
}

#if V2
E_Expression *S_ForeachRangeStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_ForeachRangeStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    if (istate->start)
	return NULL;

    E_Expression *e = NULL;
    E_Expression *elwr = lwr->interpret(istate);
    if (elwr == EXP_CANT_INTERPRET)
	return EXP_CANT_INTERPRET;

    E_Expression *eupr = upr->interpret(istate);
    if (eupr == EXP_CANT_INTERPRET)
	return EXP_CANT_INTERPRET;

    E_Expression *keysave = key->value;

    if (op == LT::TOKforeach)
    {
	key->value = elwr;

	while (1)
	{
	    e = Cmp(LT::TOKlt, key->value->type, key->value, upr);
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e->isBool(TRUE) == FALSE)
	    {	e = NULL;
		break;
	    }

	    e = body ? body->interpret(istate) : NULL;
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_BREAK_INTERPRET)
	    {   e = NULL;
		break;
	    }
	    e = Add(key->value->type, key->value, new E_IntegerExp(loc, 1, key->value->type));
	    if (e == EXP_CANT_INTERPRET)
		break;
	    key->value = e;
	}
    }
    else // LT::TOKforeach_reverse
    {
	key->value = eupr;

	while (1)
	{
	    e = Cmp(LT::TOKgt, key->value->type, key->value, lwr);
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e->isBool(TRUE) == FALSE)
	    {	e = NULL;
		break;
	    }

	    e = Min(key->value->type, key->value, new E_IntegerExp(loc, 1, key->value->type));
	    if (e == EXP_CANT_INTERPRET)
		break;
	    key->value = e;

	    e = body ? body->interpret(istate) : NULL;
	    if (e == EXP_CANT_INTERPRET)
		break;
	    if (e == EXP_BREAK_INTERPRET)
	    {   e = NULL;
		break;
	    }
	}
    }
    key->value = keysave;
    return e;
}
#endif

E_Expression *S_SwitchStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_SwitchStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    E_Expression *e = NULL;

    if (istate->start)
    {
	e = body ? body->interpret(istate) : NULL;
	if (istate->start)
	    return NULL;
	if (e == EXP_CANT_INTERPRET)
	    return e;
	if (e == EXP_BREAK_INTERPRET)
	    return NULL;
	return e;
    }


    E_Expression *econdition = condition->interpret(istate);
    if (econdition == EXP_CANT_INTERPRET)
	return EXP_CANT_INTERPRET;

    S_Statement *s = NULL;
    if (cases)
    {
	for (size_t i = 0; i < cases->dim; i++)
	{
	    S_CaseStatement *cs = (S_CaseStatement *)cases->data[i];
	    e = Equal(LT::TOKequal, Type::tint32, econdition, cs->exp);
	    if (e == EXP_CANT_INTERPRET)
		return EXP_CANT_INTERPRET;
	    if (e->isBool(TRUE))
	    {	s = cs;
		break;
	    }
	}
    }
    if (!s)
    {	if (hasNoDefault)
	    error("no default or case for %s in switch statement", econdition->oToChars());
	s = sdefault;
    }

    assert(s);
    istate->start = s;
    e = body ? body->interpret(istate) : NULL;
    assert(!istate->start);
    if (e == EXP_BREAK_INTERPRET)
	return NULL;
    return e;
}

E_Expression *S_CaseStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_CaseStatement::interpret(%s) this = %p\n", exp->oToChars(), this);
#endif
    if (istate->start == this)
	istate->start = NULL;
    if (statement)
	return statement->interpret(istate);
    else
	return NULL;
}

E_Expression *S_DefaultStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_DefaultStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    if (statement)
	return statement->interpret(istate);
    else
	return NULL;
}

E_Expression *S_GotoStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_GotoStatement::interpret()\n");
#endif
    START()
    assert(label && label->statement);
    istate->gotoTarget = label->statement;
    return EXP_GOTO_INTERPRET;
}

E_Expression *S_GotoCaseStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_GotoCaseStatement::interpret()\n");
#endif
    START()
    assert(cs);
    istate->gotoTarget = cs;
    return EXP_GOTO_INTERPRET;
}

E_Expression *S_GotoDefaultStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_GotoDefaultStatement::interpret()\n");
#endif
    START()
    assert(sw && sw->sdefault);
    istate->gotoTarget = sw->sdefault;
    return EXP_GOTO_INTERPRET;
}

E_Expression *S_LabelStatement::interpret(InterState *istate)
{
#if LOG
    printf("S_LabelStatement::interpret()\n");
#endif
    if (istate->start == this)
	istate->start = NULL;
    return statement ? statement->interpret(istate) : NULL;
}

/******************************** E_Expression ***************************/

E_Expression *E_Expression::interpret(InterState *istate)
{
#if LOG
    printf("E_Expression::interpret() %s\n", oToChars());
    printf("type = %s\n", type->oToChars());
    dump(0);
#endif
    return EXP_CANT_INTERPRET;
}

E_Expression *E_NullExp::interpret(InterState *istate)
{
    return this;
}

E_Expression *E_IntegerExp::interpret(InterState *istate)
{
#if LOG
    printf("E_IntegerExp::interpret() %s\n", oToChars());
#endif
    return this;
}

E_Expression *E_RealExp::interpret(InterState *istate)
{
#if LOG
    printf("E_RealExp::interpret() %s\n", oToChars());
#endif
    return this;
}

E_Expression *E_ComplexExp::interpret(InterState *istate)
{
    return this;
}

E_Expression *E_StringExp::interpret(InterState *istate)
{
#if LOG
    printf("E_StringExp::interpret() %s\n", oToChars());
#endif
    return this;
}

E_Expression *getVarExp(Loc loc, InterState *istate, P_Declaration *d)
{
    E_Expression *e = EXP_CANT_INTERPRET;
    P_VarDeclaration *v = d->isVarDeclaration();
    P_SymbolDeclaration *s = d->isSymbolDeclaration();
    if (v)
    {
#if V2
	if ((v->isConst() || v->isInvariant()) && v->init && !v->value)
#else
	if (v->isConst() && v->init)
#endif
	{   e = v->init->toExpression();
	    if (e && !e->type)
		e->type = v->type;
	}
	else
	{   e = v->value;
	    if (!e)
		error(loc, "variable %s is used before initialization", v->oToChars());
	    else if (e != EXP_CANT_INTERPRET)
		e = e->interpret(istate);
	}
	if (!e)
	    e = EXP_CANT_INTERPRET;
    }
    else if (s)
    {
	if (s->dsym->toInitializer() == s->sym)
	{   Expressions *exps = new Expressions();
	    e = new E_StructLiteralExp(0, s->dsym, exps);
	    e = e->semantic(NULL);
	}
    }
    return e;
}

E_Expression *E_VarExp::interpret(InterState *istate)
{
#if LOG
    printf("E_VarExp::interpret() %s\n", oToChars());
#endif
    return getVarExp(loc, istate, var);
}

E_Expression *E_DeclarationExp::interpret(InterState *istate)
{
#if LOG
    printf("E_DeclarationExp::interpret() %s\n", oToChars());
#endif
    E_Expression *e;
    P_VarDeclaration *v = declaration->isVarDeclaration();
    if (v)
    {
	P_Dsymbol *s = v->toAlias();
	if (s == v && !v->isStatic() && v->init)
	{
	    ExpInitializer *ie = v->init->isExpInitializer();
	    if (ie)
		e = ie->exp->interpret(istate);
	    else if (v->init->isVoidInitializer())
		e = NULL;
	}
#if V2
	else if (s == v && (v->isConst() || v->isInvariant()) && v->init)
#else
	else if (s == v && v->isConst() && v->init)
#endif
	{   e = v->init->toExpression();
	    if (!e)
		e = EXP_CANT_INTERPRET;
	    else if (!e->type)
		e->type = v->type;
	}
    }
    else if (declaration->isAttribDeclaration() ||
	     declaration->isTemplateMixin() ||
	     declaration->isTupleDeclaration())
    {	// These can be made to work, too lazy now
	e = EXP_CANT_INTERPRET;
    }
    else
    {	// Others should not contain executable code, so are trivial to evaluate
	e = NULL;
    }
#if LOG
    printf("-E_DeclarationExp::interpret(): %p\n", e);
#endif
    return e;
}

E_Expression *E_TupleExp::interpret(InterState *istate)
{
#if LOG
    printf("E_TupleExp::interpret() %s\n", oToChars());
#endif
    Expressions *expsx = NULL;

    for (size_t i = 0; i < exps->dim; i++)
    {   E_Expression *e = (E_Expression *)exps->data[i];
	E_Expression *ex;

	ex = e->interpret(istate);
	if (ex == EXP_CANT_INTERPRET)
	{   delete expsx;
	    return ex;
	}

	/* If any changes, do Copy On Write
	 */
	if (ex != e)
	{
	    if (!expsx)
	    {	expsx = new Expressions();
		expsx->setDim(exps->dim);
		for (size_t j = 0; j < i; j++)
		{
		    expsx->data[j] = exps->data[j];
		}
	    }
	    expsx->data[i] = (void *)ex;
	}
    }
    if (expsx)
    {	E_TupleExp *te = new E_TupleExp(loc, expsx);
	expandTuples(te->exps);
	te->type = new TypeTuple(te->exps);
	return te;
    }
    return this;
}

E_Expression *E_ArrayLiteralExp::interpret(InterState *istate)
{   Expressions *expsx = NULL;

#if LOG
    printf("E_ArrayLiteralExp::interpret() %s\n", oToChars());
#endif
    if (elements)
    {
	for (size_t i = 0; i < elements->dim; i++)
	{   E_Expression *e = (E_Expression *)elements->data[i];
	    E_Expression *ex;

	    ex = e->interpret(istate);
	    if (ex == EXP_CANT_INTERPRET)
	    {   delete expsx;
		return EXP_CANT_INTERPRET;
	    }

	    /* If any changes, do Copy On Write
	     */
	    if (ex != e)
	    {
		if (!expsx)
		{   expsx = new Expressions();
		    expsx->setDim(elements->dim);
		    for (size_t j = 0; j < elements->dim; j++)
		    {
			expsx->data[j] = elements->data[j];
		    }
		}
		expsx->data[i] = (void *)ex;
	    }
	}
    }
    if (elements && expsx)
    {
	expandTuples(expsx);
	if (expsx->dim != elements->dim)
	{   delete expsx;
	    return EXP_CANT_INTERPRET;
	}
	E_ArrayLiteralExp *ae = new E_ArrayLiteralExp(loc, expsx);
	ae->type = type;
	return ae;
    }
    return this;
}

E_Expression *E_AssocArrayLiteralExp::interpret(InterState *istate)
{   Expressions *keysx = keys;
    Expressions *valuesx = values;

#if LOG
    printf("E_AssocArrayLiteralExp::interpret() %s\n", oToChars());
#endif
    for (size_t i = 0; i < keys->dim; i++)
    {   E_Expression *ekey = (E_Expression *)keys->data[i];
	E_Expression *evalue = (E_Expression *)values->data[i];
	E_Expression *ex;

	ex = ekey->interpret(istate);
	if (ex == EXP_CANT_INTERPRET)
	    goto Lerr;

	/* If any changes, do Copy On Write
	 */
	if (ex != ekey)
	{
	    if (keysx == keys)
		keysx = (Expressions *)keys->copy();
	    keysx->data[i] = (void *)ex;
	}

	ex = evalue->interpret(istate);
	if (ex == EXP_CANT_INTERPRET)
	    goto Lerr;

	/* If any changes, do Copy On Write
	 */
	if (ex != evalue)
	{
	    if (valuesx == values)
		valuesx = (Expressions *)values->copy();
	    valuesx->data[i] = (void *)ex;
	}
    }
    if (keysx != keys)
	expandTuples(keysx);
    if (valuesx != values)
	expandTuples(valuesx);
    if (keysx->dim != valuesx->dim)
	goto Lerr;

    /* Remove duplicate keys
     */
    for (size_t i = 1; i < keysx->dim; i++)
    {   E_Expression *ekey = (E_Expression *)keysx->data[i - 1];

	for (size_t j = i; j < keysx->dim; j++)
	{   E_Expression *ekey2 = (E_Expression *)keysx->data[j];
	    E_Expression *ex = Equal(LT::TOKequal, Type::tbool, ekey, ekey2);
	    if (ex == EXP_CANT_INTERPRET)
		goto Lerr;
	    if (ex->isBool(TRUE))	// if a match
	    {
		// Remove ekey
		if (keysx == keys)
		    keysx = (Expressions *)keys->copy();
		if (valuesx == values)
		    valuesx = (Expressions *)values->copy();
		keysx->remove(i - 1);
		valuesx->remove(i - 1);
		i -= 1;		// redo the i'th iteration
		break;
	    }
	}
    }

    if (keysx != keys || valuesx != values)
    {
	E_AssocArrayLiteralExp *ae;
	ae = new E_AssocArrayLiteralExp(loc, keysx, valuesx);
	ae->type = type;
	return ae;
    }
    return this;

Lerr:
    if (keysx != keys)
	delete keysx;
    if (valuesx != values)
	delete values;
    return EXP_CANT_INTERPRET;
}

E_Expression *E_StructLiteralExp::interpret(InterState *istate)
{   Expressions *expsx = NULL;

#if LOG
    printf("E_StructLiteralExp::interpret() %s\n", oToChars());
#endif
    /* We don't know how to deal with overlapping fields
     */
    if (sd->hasUnions)
	return EXP_CANT_INTERPRET;

    if (elements)
    {
	for (size_t i = 0; i < elements->dim; i++)
	{   E_Expression *e = (E_Expression *)elements->data[i];
	    if (!e)
		continue;

	    E_Expression *ex = e->interpret(istate);
	    if (ex == EXP_CANT_INTERPRET)
	    {   delete expsx;
		return EXP_CANT_INTERPRET;
	    }

	    /* If any changes, do Copy On Write
	     */
	    if (ex != e)
	    {
		if (!expsx)
		{   expsx = new Expressions();
		    expsx->setDim(elements->dim);
		    for (size_t j = 0; j < elements->dim; j++)
		    {
			expsx->data[j] = elements->data[j];
		    }
		}
		expsx->data[i] = (void *)ex;
	    }
	}
    }
    if (elements && expsx)
    {
	expandTuples(expsx);
	if (expsx->dim != elements->dim)
	{   delete expsx;
	    return EXP_CANT_INTERPRET;
	}
	E_StructLiteralExp *se = new E_StructLiteralExp(loc, sd, expsx);
	se->type = type;
	return se;
    }
    return this;
}

E_Expression *E_UnaExp::interpretCommon(InterState *istate, E_Expression *(*fp)(Type *, E_Expression *))
{   E_Expression *e;
    E_Expression *e1;

#if LOG
    printf("E_UnaExp::interpretCommon() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e1->isConst() != 1)
	goto Lcant;

    e = (*fp)(type, e1);
    return e;

Lcant:
    return EXP_CANT_INTERPRET;
}

#define UNA_INTERPRET(op) \
	E_Expression *E_##op##Exp::interpret(InterState *istate)	\
{							\
    return interpretCommon(istate, &op);		\
}

UNA_INTERPRET(Neg)
UNA_INTERPRET(Com)
UNA_INTERPRET(Not)
UNA_INTERPRET(Bool)


typedef E_Expression *(*fp_t)(Type *, E_Expression *, E_Expression *);

E_Expression *E_BinExp::interpretCommon(InterState *istate, fp_t fp)
{   E_Expression *e;
    E_Expression *e1;
    E_Expression *e2;

#if LOG
    printf("E_BinExp::interpretCommon() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e1->isConst() != 1)
	goto Lcant;

    e2 = this->e2->interpret(istate);
    if (e2 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e2->isConst() != 1)
	goto Lcant;

    e = (*fp)(type, e1, e2);
    return e;

Lcant:
    return EXP_CANT_INTERPRET;
}

#define BIN_INTERPRET(op) \
	E_Expression *E_##op##Exp::interpret(InterState *istate)	\
{							\
    return interpretCommon(istate, &op);		\
}

BIN_INTERPRET(Add)
BIN_INTERPRET(Min)
BIN_INTERPRET(Mul)
BIN_INTERPRET(Div)
BIN_INTERPRET(Mod)
BIN_INTERPRET(Shl)
BIN_INTERPRET(Shr)
BIN_INTERPRET(Ushr)
BIN_INTERPRET(And)
BIN_INTERPRET(Or)
BIN_INTERPRET(Xor)


typedef E_Expression *(*fp2_t)(LT::T, Type *, E_Expression *, E_Expression *);

E_Expression *E_BinExp::interpretCommon2(InterState *istate, fp2_t fp)
{   E_Expression *e;
    E_Expression *e1;
    E_Expression *e2;

#if LOG
    printf("E_BinExp::interpretCommon2() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e1->isConst() != 1 &&
	e1->op != LT::TOKnull &&
	e1->op != LT::TOKstring &&
	e1->op != LT::TOKarrayliteral &&
	e1->op != LT::TOKstructliteral)
	goto Lcant;

    e2 = this->e2->interpret(istate);
    if (e2 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e2->isConst() != 1 &&
	e2->op != LT::TOKnull &&
	e2->op != LT::TOKstring &&
	e2->op != LT::TOKarrayliteral &&
	e2->op != LT::TOKstructliteral)
	goto Lcant;

    e = (*fp)(op, type, e1, e2);
    return e;

Lcant:
    return EXP_CANT_INTERPRET;
}

#define BIN_INTERPRET2(op) \
	E_Expression *E_##op##Exp::interpret(InterState *istate)	\
{							\
    return interpretCommon2(istate, &op);		\
}

BIN_INTERPRET2(Equal)
BIN_INTERPRET2(Identity)
BIN_INTERPRET2(Cmp)

E_Expression *E_BinExp::interpretAssignCommon(InterState *istate, fp_t fp, int post)
{
#if LOG
    printf("E_BinExp::interpretAssignCommon() %s\n", oToChars());
#endif
    E_Expression *e = EXP_CANT_INTERPRET;
    E_Expression *e1 = this->e1;

    if (fp)
    {
	if (e1->op == LT::TOKcast)
	{   E_CastExp *ce = (E_CastExp *)e1;
	    e1 = ce->e1;
	}
    }
    if (e1 == EXP_CANT_INTERPRET)
	return e1;
    E_Expression *e2 = this->e2->interpret(istate);
    if (e2 == EXP_CANT_INTERPRET)
	return e2;

    /* Assignment to variable of the form:
     *	v = e2
     */
    if (e1->op == LT::TOKvar)
    {
	E_VarExp *ve = (E_VarExp *)e1;
	P_VarDeclaration *v = ve->var->isVarDeclaration();
	if (v && !v->isDataseg())
	{
	    /* Chase down rebinding of out and ref
	     */
	    if (v->value && v->value->op == LT::TOKvar)
	    {
		E_VarExp *ve2 = (E_VarExp *)v->value;
		if (ve2->var->isSymbolDeclaration())
		{
		    /* This can happen if v is a struct initialized to
		     * 0 using an __initZ P_SymbolDeclaration from
		     * TypeStruct::defaultInit()
		     */
		}
		else
		    v = ve2->var->isVarDeclaration();
		assert(v);
	    }

	    E_Expression *ev = v->value;
	    if (fp && !ev)
	    {	error("variable %s is used before initialization", v->oToChars());
		return e;
	    }
	    if (fp)
		e2 = (*fp)(v->type, ev, e2);
	    else
	    {	/* Look for special case of struct being initialized with 0.
		 */
		if (v->type->toBasetype()->ty == Tstruct && e2->op == LT::TOKint64)
		{
		    e2 = v->type->defaultInit();
		}
		e2 = Cast(v->type, v->type, e2);
	    }
	    if (e2 != EXP_CANT_INTERPRET)
	    {
		if (!v->isParameter())
		{
		    for (size_t i = 0; 1; i++)
		    {
			if (i == istate->vars.dim)
			{   istate->vars.push(v);
			    //printf("\tadding %s to istate\n", v->oToChars());
			    break;
			}
			if (v == (P_VarDeclaration *)istate->vars.data[i])
			    break;
		    }
		}
		v->value = e2;
		e = Cast(type, type, post ? ev : e2);
	    }
	}
    }
    /* Assignment to struct member of the form:
     *   v.var = e2
     */
    else if (e1->op == LT::TOKdotvar && ((E_DotVarExp *)e1)->e1->op == LT::TOKvar)
    {	E_VarExp *ve = (E_VarExp *)((E_DotVarExp *)e1)->e1;
	P_VarDeclaration *v = ve->var->isVarDeclaration();

	if (v->isDataseg())
	    return EXP_CANT_INTERPRET;
	if (fp && !v->value)
	{   error("variable %s is used before initialization", v->oToChars());
	    return e;
	}
	E_Expression *vie = v->value;
	if (vie->op == LT::TOKvar)
	{
	    P_Declaration *d = ((E_VarExp *)vie)->var;
	    vie = getVarExp(e1->loc, istate, d);
	}
	if (vie->op != LT::TOKstructliteral)
	    return EXP_CANT_INTERPRET;
	E_StructLiteralExp *se = (E_StructLiteralExp *)vie;
	P_VarDeclaration *vf = ((E_DotVarExp *)e1)->var->isVarDeclaration();
	if (!vf)
	    return EXP_CANT_INTERPRET;
	int fieldi = se->getFieldIndex(type, vf->offset);
	if (fieldi == -1)
	    return EXP_CANT_INTERPRET;
	E_Expression *ev = se->getField(type, vf->offset);
	if (fp)
	    e2 = (*fp)(type, ev, e2);
	else
	    e2 = Cast(type, type, e2);
	if (e2 == EXP_CANT_INTERPRET)
	    return e2;

	if (!v->isParameter())
	{
	    for (size_t i = 0; 1; i++)
	    {
		if (i == istate->vars.dim)
		{   istate->vars.push(v);
		    break;
		}
		if (v == (P_VarDeclaration *)istate->vars.data[i])
		    break;
	    }
	}

	/* Create new struct literal reflecting updated fieldi
	 */
	Expressions *expsx = new Expressions();
	expsx->setDim(se->elements->dim);
	for (size_t j = 0; j < expsx->dim; j++)
	{
	    if (j == fieldi)
		expsx->data[j] = (void *)e2;
	    else
		expsx->data[j] = se->elements->data[j];
	}
	v->value = new E_StructLiteralExp(se->loc, se->sd, expsx);
	v->value->type = se->type;

	e = Cast(type, type, post ? ev : e2);
    }
    /* Assignment to struct member of the form:
     *   *(symoffexp) = e2
     */
    else if (e1->op == LT::TOKstar && ((E_PtrExp *)e1)->e1->op == LT::TOKsymoff)
    {	E_SymOffExp *soe = (E_SymOffExp *)((E_PtrExp *)e1)->e1;
	P_VarDeclaration *v = soe->var->isVarDeclaration();

	if (v->isDataseg())
	    return EXP_CANT_INTERPRET;
	if (fp && !v->value)
	{   error("variable %s is used before initialization", v->oToChars());
	    return e;
	}
	E_Expression *vie = v->value;
	if (vie->op == LT::TOKvar)
	{
	    P_Declaration *d = ((E_VarExp *)vie)->var;
	    vie = getVarExp(e1->loc, istate, d);
	}
	if (vie->op != LT::TOKstructliteral)
	    return EXP_CANT_INTERPRET;
	E_StructLiteralExp *se = (E_StructLiteralExp *)vie;
	int fieldi = se->getFieldIndex(type, soe->offset);
	if (fieldi == -1)
	    return EXP_CANT_INTERPRET;
	E_Expression *ev = se->getField(type, soe->offset);
	if (fp)
	    e2 = (*fp)(type, ev, e2);
	else
	    e2 = Cast(type, type, e2);
	if (e2 == EXP_CANT_INTERPRET)
	    return e2;

	if (!v->isParameter())
	{
	    for (size_t i = 0; 1; i++)
	    {
		if (i == istate->vars.dim)
		{   istate->vars.push(v);
		    break;
		}
		if (v == (P_VarDeclaration *)istate->vars.data[i])
		    break;
	    }
	}

	/* Create new struct literal reflecting updated fieldi
	 */
	Expressions *expsx = new Expressions();
	expsx->setDim(se->elements->dim);
	for (size_t j = 0; j < expsx->dim; j++)
	{
	    if (j == fieldi)
		expsx->data[j] = (void *)e2;
	    else
		expsx->data[j] = se->elements->data[j];
	}
	v->value = new E_StructLiteralExp(se->loc, se->sd, expsx);
	v->value->type = se->type;

	e = Cast(type, type, post ? ev : e2);
    }
    /* Assignment to array element of the form:
     *   a[i] = e2
     */
    else if (e1->op == LT::TOKindex && ((E_IndexExp *)e1)->e1->op == LT::TOKvar)
    {	E_IndexExp *ie = (E_IndexExp *)e1;
	E_VarExp *ve = (E_VarExp *)ie->e1;
	P_VarDeclaration *v = ve->var->isVarDeclaration();

	if (!v || v->isDataseg())
	    return EXP_CANT_INTERPRET;
	if (!v->value)
	{
	    if (fp)
	    {   error("variable %s is used before initialization", v->oToChars());
		return e;
	    }

	    Type *t = v->type->toBasetype();
	    if (t->ty == Tsarray)
	    {
		/* This array was void initialized. Create a
		 * default initializer for it.
		 * What we should do is fill the array literal with
		 * NULL data, so use-before-initialized can be detected.
		 * But we're too lazy at the moment to do it, as that
		 * involves redoing Index() and whoever calls it.
		 */
		E_Expression *ev = v->type->defaultInit();
		size_t dim = ((TypeSArray *)t)->dim->toInteger();
		Expressions *elements = new Expressions();
		elements->setDim(dim);
		for (size_t i = 0; i < dim; i++)
		    elements->data[i] = (void *)ev;
		E_ArrayLiteralExp *ae = new E_ArrayLiteralExp(0, elements);
		ae->type = v->type;
		v->value = ae;
	    }
	    else
		return EXP_CANT_INTERPRET;
	}

	E_ArrayLiteralExp *ae = NULL;
	E_AssocArrayLiteralExp *aae = NULL;
	E_StringExp *se = NULL;
	if (v->value->op == LT::TOKarrayliteral)
	    ae = (E_ArrayLiteralExp *)v->value;
	else if (v->value->op == LT::TOKassocarrayliteral)
	    aae = (E_AssocArrayLiteralExp *)v->value;
	else if (v->value->op == LT::TOKstring)
	    se = (E_StringExp *)v->value;
	else
	    return EXP_CANT_INTERPRET;

	E_Expression *index = ie->e2->interpret(istate);
	if (index == EXP_CANT_INTERPRET)
	    return EXP_CANT_INTERPRET;
	E_Expression *ev;
	if (fp || ae || se)	// not for aae, because key might not be there
	{
	    ev = Index(type, v->value, index);
	    if (ev == EXP_CANT_INTERPRET)
		return EXP_CANT_INTERPRET;
	}

	if (fp)
	    e2 = (*fp)(type, ev, e2);
	else
	    e2 = Cast(type, type, e2);
	if (e2 == EXP_CANT_INTERPRET)
	    return e2;

	if (!v->isParameter())
	{
	    for (size_t i = 0; 1; i++)
	    {
		if (i == istate->vars.dim)
		{   istate->vars.push(v);
		    break;
		}
		if (v == (P_VarDeclaration *)istate->vars.data[i])
		    break;
	    }
	}

	if (ae)
	{
	    /* Create new array literal reflecting updated elem
	     */
	    int elemi = index->toInteger();
	    Expressions *expsx = new Expressions();
	    expsx->setDim(ae->elements->dim);
	    for (size_t j = 0; j < expsx->dim; j++)
	    {
		if (j == elemi)
		    expsx->data[j] = (void *)e2;
		else
		    expsx->data[j] = ae->elements->data[j];
	    }
	    v->value = new E_ArrayLiteralExp(ae->loc, expsx);
	    v->value->type = ae->type;
	}
	else if (aae)
	{
	    /* Create new associative array literal reflecting updated key/value
	     */
	    Expressions *keysx = aae->keys;
	    Expressions *valuesx = new Expressions();
	    valuesx->setDim(aae->values->dim);
	    int updated = 0;
	    for (size_t j = valuesx->dim; j; )
	    {	j--;
		E_Expression *ekey = (E_Expression *)aae->keys->data[j];
		E_Expression *ex = Equal(LT::TOKequal, Type::tbool, ekey, index);
		if (ex == EXP_CANT_INTERPRET)
		    return EXP_CANT_INTERPRET;
		if (ex->isBool(TRUE))
		{   valuesx->data[j] = (void *)e2;
		    updated = 1;
		}
		else
		    valuesx->data[j] = aae->values->data[j];
	    }
	    if (!updated)
	    {	// Append index/e2 to keysx[]/valuesx[]
		valuesx->push(e2);
		keysx = (Expressions *)keysx->copy();
		keysx->push(index);
	    }
	    v->value = new E_AssocArrayLiteralExp(aae->loc, keysx, valuesx);
	    v->value->type = aae->type;
	}
	else if (se)
	{
	    /* Create new string literal reflecting updated elem
	     */
	    int elemi = index->toInteger();
	    unsigned char *s;
	    s = (unsigned char *)mem.calloc(se->len + 1, se->sz);
	    memcpy(s, se->string, se->len * se->sz);
	    unsigned value = e2->toInteger();
	    switch (se->sz)
	    {
		case 1:	s[elemi] = value; break;
		case 2:	((unsigned short *)s)[elemi] = value; break;
		case 4:	((unsigned *)s)[elemi] = value; break;
		default:
		    assert(0);
		    break;
	    }
	    E_StringExp *se2 = new E_StringExp(se->loc, s, se->len);
	    se2->committed = se->committed;
	    se2->postfix = se->postfix;
	    se2->type = se->type;
	    v->value = se2;
	}
	else
	    assert(0);

	e = Cast(type, type, post ? ev : e2);
    }
    else
    {
#ifdef DEBUG
	dump(0);
#endif
    }
    return e;
}

E_Expression *E_AssignExp::interpret(InterState *istate)
{
    return interpretAssignCommon(istate, NULL);
}

#define BIN_ASSIGN_INTERPRET(op) \
	E_Expression *E_##op##AssignExp::interpret(InterState *istate)	\
{								\
    return interpretAssignCommon(istate, &op);			\
}

BIN_ASSIGN_INTERPRET(Add)
BIN_ASSIGN_INTERPRET(Min)
BIN_ASSIGN_INTERPRET(Cat)
BIN_ASSIGN_INTERPRET(Mul)
BIN_ASSIGN_INTERPRET(Div)
BIN_ASSIGN_INTERPRET(Mod)
BIN_ASSIGN_INTERPRET(Shl)
BIN_ASSIGN_INTERPRET(Shr)
BIN_ASSIGN_INTERPRET(Ushr)
BIN_ASSIGN_INTERPRET(And)
BIN_ASSIGN_INTERPRET(Or)
BIN_ASSIGN_INTERPRET(Xor)

E_Expression *E_PostExp::interpret(InterState *istate)
{
#if LOG
    printf("E_PostExp::interpret() %s\n", oToChars());
#endif
    E_Expression *e;
    if (op == LT::TOKplusplus)
	e = interpretAssignCommon(istate, &Add, 1);
    else
	e = interpretAssignCommon(istate, &Min, 1);
#if LOG
    if (e == EXP_CANT_INTERPRET)
	printf("E_PostExp::interpret() CANT\n");
#endif
    return e;
}

E_Expression *E_AndAndExp::interpret(InterState *istate)
{
#if LOG
    printf("E_AndAndExp::interpret() %s\n", oToChars());
#endif
    E_Expression *e = e1->interpret(istate);
    if (e != EXP_CANT_INTERPRET)
    {
	if (e->isBool(FALSE))
	    e = new E_IntegerExp(e1->loc, 0, type);
	else if (e->isBool(TRUE))
	{
	    e = e2->interpret(istate);
	    if (e != EXP_CANT_INTERPRET)
	    {
		if (e->isBool(FALSE))
		    e = new E_IntegerExp(e1->loc, 0, type);
		else if (e->isBool(TRUE))
		    e = new E_IntegerExp(e1->loc, 1, type);
		else
		    e = EXP_CANT_INTERPRET;
	    }
	}
	else
	    e = EXP_CANT_INTERPRET;
    }
    return e;
}

E_Expression *E_OrOrExp::interpret(InterState *istate)
{
#if LOG
    printf("E_OrOrExp::interpret() %s\n", oToChars());
#endif
    E_Expression *e = e1->interpret(istate);
    if (e != EXP_CANT_INTERPRET)
    {
	if (e->isBool(TRUE))
	    e = new E_IntegerExp(e1->loc, 1, type);
	else if (e->isBool(FALSE))
	{
	    e = e2->interpret(istate);
	    if (e != EXP_CANT_INTERPRET)
	    {
		if (e->isBool(FALSE))
		    e = new E_IntegerExp(e1->loc, 0, type);
		else if (e->isBool(TRUE))
		    e = new E_IntegerExp(e1->loc, 1, type);
		else
		    e = EXP_CANT_INTERPRET;
	    }
	}
	else
	    e = EXP_CANT_INTERPRET;
    }
    return e;
}


E_Expression *E_CallExp::interpret(InterState *istate)
{   E_Expression *e = EXP_CANT_INTERPRET;

#if LOG
    printf("E_CallExp::interpret() %s\n", oToChars());
#endif
    if (e1->op == LT::TOKvar)
    {
	P_FuncDeclaration *fd = ((E_VarExp *)e1)->var->isFuncDeclaration();
	if (fd)
	{
#if V2
	    enum BUILTIN b = fd->isBuiltin();
	    if (b)
	    {	Expressions args;
		args.setDim(arguments->dim);
		for (size_t i = 0; i < args.dim; i++)
		{
		    E_Expression *earg = (E_Expression *)arguments->data[i];
		    earg = earg->interpret(istate);
		    if (earg == EXP_CANT_INTERPRET)
			return earg;
		    args.data[i] = (void *)earg;
		}
		e = eval_builtin(b, &args);
		if (!e)
		    e = EXP_CANT_INTERPRET;
	    }
	    else
#endif
	    // Inline .dup
	    if (fd->ident == Id::adDup && arguments && arguments->dim == 2)
	    {
		e = (E_Expression *)arguments->data[1];
		e = e->interpret(istate);
		if (e != EXP_CANT_INTERPRET)
		{
		    e = expType(type, e);
		}
	    }
	    else
	    {
		E_Expression *eresult = fd->interpret(istate, arguments);
		if (eresult)
		    e = eresult;
		else if (fd->type->toBasetype()->nextOf()->ty == Tvoid && !global.errors)
		    e = EXP_VOID_INTERPRET;
		else
		    error("cannot evaluate %s at compile time", oToChars());
	    }
	}
    }
    return e;
}

E_Expression *E_CommaExp::interpret(InterState *istate)
{
#if LOG
    printf("E_CommaExp::interpret() %s\n", oToChars());
#endif
    E_Expression *e = e1->interpret(istate);
    if (e != EXP_CANT_INTERPRET)
	e = e2->interpret(istate);
    return e;
}

E_Expression *E_CondExp::interpret(InterState *istate)
{
#if LOG
    printf("E_CondExp::interpret() %s\n", oToChars());
#endif
    E_Expression *e = econd->interpret(istate);
    if (e != EXP_CANT_INTERPRET)
    {
	if (e->isBool(TRUE))
	    e = e1->interpret(istate);
	else if (e->isBool(FALSE))
	    e = e2->interpret(istate);
	else
	    e = EXP_CANT_INTERPRET;
    }
    return e;
}

E_Expression *E_ArrayLengthExp::interpret(InterState *istate)
{   E_Expression *e;
    E_Expression *e1;

#if LOG
    printf("E_ArrayLengthExp::interpret() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e1->op == LT::TOKstring || e1->op == LT::TOKarrayliteral || e1->op == LT::TOKassocarrayliteral)
    {
	e = ArrayLength(type, e1);
    }
    else if (e1->op == LT::TOKnull)
    {
	e = new E_IntegerExp(loc, 0, type);
    }
    else
	goto Lcant;
    return e;

Lcant:
    return EXP_CANT_INTERPRET;
}

E_Expression *E_IndexExp::interpret(InterState *istate)
{   E_Expression *e;
    E_Expression *e1;
    E_Expression *e2;

#if LOG
    printf("E_IndexExp::interpret() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;

    if (e1->op == LT::TOKstring || e1->op == LT::TOKarrayliteral)
    {
	/* Set the $ variable
	 */
	e = ArrayLength(Type::tsize_t, e1);
	if (e == EXP_CANT_INTERPRET)
	    goto Lcant;
	if (lengthVar)
	    lengthVar->value = e;
    }

    e2 = this->e2->interpret(istate);
    if (e2 == EXP_CANT_INTERPRET)
	goto Lcant;
    return Index(type, e1, e2);

Lcant:
    return EXP_CANT_INTERPRET;
}


E_Expression *E_SliceExp::interpret(InterState *istate)
{   E_Expression *e;
    E_Expression *e1;
    E_Expression *lwr;
    E_Expression *upr;

#if LOG
    printf("E_SliceExp::interpret() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (!this->lwr)
    {
	e = e1->castTo(NULL, type);
	return e->interpret(istate);
    }

    /* Set the $ variable
     */
    e = ArrayLength(Type::tsize_t, e1);
    if (e == EXP_CANT_INTERPRET)
	goto Lcant;
    if (lengthVar)
	lengthVar->value = e;

    /* Evaluate lower and upper bounds of slice
     */
    lwr = this->lwr->interpret(istate);
    if (lwr == EXP_CANT_INTERPRET)
	goto Lcant;
    upr = this->upr->interpret(istate);
    if (upr == EXP_CANT_INTERPRET)
	goto Lcant;

    return Slice(type, e1, lwr, upr);

Lcant:
    return EXP_CANT_INTERPRET;
}


E_Expression *E_CatExp::interpret(InterState *istate)
{   E_Expression *e;
    E_Expression *e1;
    E_Expression *e2;

#if LOG
    printf("E_CatExp::interpret() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
    {
	goto Lcant;
    }
    e2 = this->e2->interpret(istate);
    if (e2 == EXP_CANT_INTERPRET)
	goto Lcant;
    return Cat(type, e1, e2);

Lcant:
#if LOG
    printf("E_CatExp::interpret() %s CANT\n", oToChars());
#endif
    return EXP_CANT_INTERPRET;
}


E_Expression *E_CastExp::interpret(InterState *istate)
{   E_Expression *e;
    E_Expression *e1;

#if LOG
    printf("E_CastExp::interpret() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    return Cast(type, to, e1);

Lcant:
#if LOG
    printf("E_CastExp::interpret() %s CANT\n", oToChars());
#endif
    return EXP_CANT_INTERPRET;
}


E_Expression *E_AssertExp::interpret(InterState *istate)
{   E_Expression *e;
    E_Expression *e1;

#if LOG
    printf("E_AssertExp::interpret() %s\n", oToChars());
#endif
    e1 = this->e1->interpret(istate);
    if (e1 == EXP_CANT_INTERPRET)
	goto Lcant;
    if (e1->isBool(TRUE))
    {
    }
    else if (e1->isBool(FALSE))
    {
	if (msg)
	{
	    e = msg->interpret(istate);
	    if (e == EXP_CANT_INTERPRET)
		goto Lcant;
	    error("%s", e->oToChars());
	}
	else
	    error("%s failed", oToChars());
	goto Lcant;
    }
    else
	goto Lcant;
    return e1;

Lcant:
    return EXP_CANT_INTERPRET;
}

E_Expression *E_PtrExp::interpret(InterState *istate)
{   E_Expression *e = EXP_CANT_INTERPRET;

#if LOG
    printf("E_PtrExp::interpret() %s\n", oToChars());
#endif

    // Constant fold *(&structliteral + offset)
    if (e1->op == LT::TOKadd)
    {	E_AddExp *ae = (E_AddExp *)e1;
	if (ae->e1->op == LT::TOKaddress && ae->e2->op == LT::TOKint64)
	{   E_AddrExp *ade = (E_AddrExp *)ae->e1;
	    E_Expression *ex = ade->e1;
	    ex = ex->interpret(istate);
	    if (ex != EXP_CANT_INTERPRET)
	    {
		if (ex->op == LT::TOKstructliteral)
		{   E_StructLiteralExp *se = (E_StructLiteralExp *)ex;
		    unsigned offset = ae->e2->toInteger();
		    e = se->getField(type, offset);
		    if (!e)
			e = EXP_CANT_INTERPRET;
		    return e;
		}
	    }
	}
	e = Ptr(type, e1);
    }
    else if (e1->op == LT::TOKsymoff)
    {	E_SymOffExp *soe = (E_SymOffExp *)e1;
	P_VarDeclaration *v = soe->var->isVarDeclaration();
	if (v)
	{   E_Expression *ev = getVarExp(loc, istate, v);
	    if (ev != EXP_CANT_INTERPRET && ev->op == LT::TOKstructliteral)
	    {	E_StructLiteralExp *se = (E_StructLiteralExp *)ev;
		e = se->getField(type, soe->offset);
		if (!e)
		    e = EXP_CANT_INTERPRET;
	    }
	}
    }
#if LOG
    if (e == EXP_CANT_INTERPRET)
	printf("E_PtrExp::interpret() %s = EXP_CANT_INTERPRET\n", oToChars());
#endif
    return e;
}

E_Expression *E_DotVarExp::interpret(InterState *istate)
{   E_Expression *e = EXP_CANT_INTERPRET;

#if LOG
    printf("E_DotVarExp::interpret() %s\n", oToChars());
#endif

    E_Expression *ex = e1->interpret(istate);
    if (ex != EXP_CANT_INTERPRET)
    {
	if (ex->op == LT::TOKstructliteral)
	{   E_StructLiteralExp *se = (E_StructLiteralExp *)ex;
	    P_VarDeclaration *v = var->isVarDeclaration();
	    if (v)
	    {	e = se->getField(type, v->offset);
		if (!e)
		    e = EXP_CANT_INTERPRET;
		return e;
	    }
	}
    }

#if LOG
    if (e == EXP_CANT_INTERPRET)
	printf("E_DotVarExp::interpret() %s = EXP_CANT_INTERPRET\n", oToChars());
#endif
    return e;
}

/******************************* Special Functions ***************************/

E_Expression *interpret_aaLen(InterState *istate, Expressions *arguments)
{
    if (!arguments || arguments->dim != 1)
	return NULL;
    E_Expression *earg = (E_Expression *)arguments->data[0];
    earg = earg->interpret(istate);
    if (earg == EXP_CANT_INTERPRET)
	return NULL;
    if (earg->op != LT::TOKassocarrayliteral)
	return NULL;
    E_AssocArrayLiteralExp *aae = (E_AssocArrayLiteralExp *)earg;
    E_Expression *e = new E_IntegerExp(aae->loc, aae->keys->dim, Type::tsize_t);
    return e;
}

E_Expression *interpret_aaKeys(InterState *istate, Expressions *arguments)
{
    //printf("interpret_aaKeys()\n");
    if (!arguments || arguments->dim != 2)
	return NULL;
    E_Expression *earg = (E_Expression *)arguments->data[0];
    earg = earg->interpret(istate);
    if (earg == EXP_CANT_INTERPRET)
	return NULL;
    if (earg->op != LT::TOKassocarrayliteral)
	return NULL;
    E_AssocArrayLiteralExp *aae = (E_AssocArrayLiteralExp *)earg;
    E_Expression *e = new E_ArrayLiteralExp(aae->loc, aae->keys);
    return e;
}

E_Expression *interpret_aaValues(InterState *istate, Expressions *arguments)
{
    //printf("interpret_aaValues()\n");
    if (!arguments || arguments->dim != 3)
	return NULL;
    E_Expression *earg = (E_Expression *)arguments->data[0];
    earg = earg->interpret(istate);
    if (earg == EXP_CANT_INTERPRET)
	return NULL;
    if (earg->op != LT::TOKassocarrayliteral)
	return NULL;
    E_AssocArrayLiteralExp *aae = (E_AssocArrayLiteralExp *)earg;
    E_Expression *e = new E_ArrayLiteralExp(aae->loc, aae->values);
    //printf("result is %s\n", e->oToChars());
    return e;
}

